<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 地标和路径管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4e73df;
            --secondary-color: #858796;
            --success-color: #1cc88a;
            --info-color: #36b9cc;
            --warning-color: #f6c23e;
            --danger-color: #e74a3b;
            --light-color: #f8f9fc;
            --dark-color: #5a5c69;
        }

        * {
            font-family: 'Noto Sans SC', sans-serif;
        }

        body {
            background-color: var(--light-color);
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .login-container {
            max-width: 400px;
            width: 100%;
            padding: 30px;
            background-color: white;
            border-radius: 0.5rem;
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header i {
            font-size: 3rem;
            color: var(--primary-color);
        }

        .login-header h1 {
            font-size: 1.5rem;
            font-weight: 700;
            color: var(--primary-color);
            margin-top: 10px;
        }

        .form-floating {
            margin-bottom: 20px;
        }

        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.25rem rgba(78, 115, 223, 0.25);
        }

        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
            width: 100%;
            padding: 10px;
            font-weight: 500;
        }

        .btn-primary:hover {
            background-color: #2e59d9;
            border-color: #2653d4;
        }

        .alert {
            display: none;
            margin-bottom: 20px;
        }

        .system-info {
            text-align: center;
            margin-top: 20px;
            color: var(--secondary-color);
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <i class="bi bi-geo-alt-fill"></i>
            <h1>地标和路径管理系统</h1>
        </div>

        <div class="alert alert-danger" id="error-message" role="alert">
            用户名或密码错误，请重试。
        </div>

        <form id="login-form">
            <div class="form-floating mb-3">
                <input type="text" class="form-control" id="username" placeholder="用户名" required>
                <label for="username">用户名</label>
            </div>

            <div class="form-floating mb-3">
                <input type="password" class="form-control" id="password" placeholder="密码" required>
                <label for="password">密码</label>
            </div>

            <div class="d-grid gap-2">
                <button class="btn btn-primary" type="submit">
                    <i class="bi bi-box-arrow-in-right me-2"></i>登录
                </button>
            </div>
        </form>

        <div class="system-info">
            <p>初始账号：jftadmin</p>
            <p>初始密码：123456</p>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#login-form').on('submit', function(e) {
                e.preventDefault();

                const username = $('#username').val();
                const password = $('#password').val();

                // 发送登录请求
                $.ajax({
                    url: '/token',
                    type: 'POST',
                    data: {
                        username: username,
                        password: password
                    },
                    success: function(response) {
                        // 保存令牌到localStorage
                        localStorage.setItem('access_token', response.access_token);
                        // 设置cookie，以便后端中间件可以验证
                        document.cookie = `access_token=${response.access_token}; path=/; max-age=${30*60}`; // 30分钟过期
                        // 重定向到管理页面
                        window.location.href = '/admin';
                    },
                    error: function() {
                        // 显示错误消息
                        $('#error-message').show();
                    }
                });
            });
        });
    </script>
</body>
</html>
